<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
  let data = [
    {id: 1, name: '女装', parent_id: 0},
    {id: 2, name: '数码', parent_id: 0},
    {id: 3, name: '半身裙', parent_id: 1},
    {id: 4, name: 'A字群', parent_id: 1},
    {id: 5, name: '电脑配件', parent_id: 2},
    {id: 6, name: '内存', parent_id: 5},
    {id: 7, name: '连衣裙', parent_id: 2}
  ]
  function convert(data, parentId) {
    let convertData = []
    data.forEach((item, index) => {
      if(item.parent_id === parentId) {
        convertData.push(item)
        convertChild(data, item, item.id)
      }
    })
    return convertData
  }
  function convertChild(arr, parentItem, parentId) {
    parentItem.children = parentItem.children ? parentItem.children : []
    arr.forEach(item => {
      if(item.parent_id === parentId) {
        parentItem.children.push(item)
        convertChild(arr, item ,item.id)
      }
    })
    return parentItem.children
  }
  console.log(convert(data, 0))
</script>
</html>